package com.chijun.mform.process.dapater;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chijun.mform.FormBuilder;
import com.chijun.mform.R;
import com.chijun.mform.create.fragment.FormAddProcessFragment;
import com.chijun.mform.create.mode.FormApproveProcess;
import com.chijun.mform.full.mode.ModeFormFullApprovePerson;
import com.example.ccj.b.c.custom.image.LoadImagesRecycler;
import com.example.ccj.b.c.custom.image.MLocalMedia;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.tengxun.cos.COS;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 审批人审批结果
 * 2024年11月10日23:38:06
 */
public class ApprovePersonResultAdapter extends RecyclerView.Adapter<ApprovePersonResultAdapter.ViewHolder>{

    List<ModeFormFullApprovePerson> personList;
    Map<Long, FormApproveProcess> approveProcessMap;

    /**
     * 字段类型数组
     *
     */
    public ApprovePersonResultAdapter(List<ModeFormFullApprovePerson> personList, Map<Long, FormApproveProcess> approveProcessMap){
        this.personList = personList;
        this.approveProcessMap = approveProcessMap;
    }

    @NonNull
    @Override
    public ApprovePersonResultAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ApprovePersonResultAdapter.ViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_approve_person_result, parent, false));
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ApprovePersonResultAdapter.ViewHolder holder, int position) {
        //当前加载审批人
        ModeFormFullApprovePerson formApprovePerson = personList.get(position);
        //当前审批人所属流程
        FormApproveProcess formApproveProcess = approveProcessMap.get(formApprovePerson.processId);

        //审批人名称
        if (StringUtils.isNoEmpty(formApprovePerson.personName)) holder.personName.setText(formApprovePerson.personName);

        //流程名称
        if (formApproveProcess != null) {
            if (formApprovePerson.state == FormAddProcessFragment.SHEN_PI_STATE_PING_LUN) {
                holder.processName.setText("评论" + formApprovePerson.personName);
                holder.rl.setBackgroundResource(R.drawable.process_ping_lun_tou_xiang);
            } else if (StringUtils.isNoEmpty(formApproveProcess.name)) {
                holder.processName.setText(formApproveProcess.name + formApprovePerson.personName);
            } else {
                if (formApproveProcess.type == FormAddProcessFragment.FA_QI_REN_JIE_DIAN) {
                    holder.processName.setText("发起人" + formApprovePerson.personName);
                } else if (formApproveProcess.type == FormAddProcessFragment.SHEN_PI_REN_JIE_DIAN) {
                    holder.processName.setText("审批人" + formApprovePerson.personName);
                } else if (formApproveProcess.type == FormAddProcessFragment.CHAO_SONG_REN_JIE_DIAN) {
                    holder.processName.setText("抄送人" + formApprovePerson.personName);
                }
                //根据不同的审批类型更换人员头像的背景色
                if (formApproveProcess.type == FormAddProcessFragment.FA_QI_REN_JIE_DIAN) {
                    holder.rl.setBackgroundResource(R.drawable.process_fa_qi_ren_tou_xiang);
                } else if (formApproveProcess.type == FormAddProcessFragment.SHEN_PI_REN_JIE_DIAN) {
                    holder.rl.setBackgroundResource(R.drawable.process_shen_pi_ren_tou_xiang);
                } else if (formApproveProcess.type == FormAddProcessFragment.CHAO_SONG_REN_JIE_DIAN) {
                    holder.rl.setBackgroundResource(R.drawable.process_chao_song_ren_tou_xiang);
                }
            }
        }

        //审批状态
        //等待
        if (formApprovePerson.state == FormAddProcessFragment.SHEN_PI_STATE_DENG_DAI) {
            //等待不加载审批意见及图片时间
            holder.layout.setVisibility(View.GONE);
            holder.time.setVisibility(View.GONE);
            //审批状态图片
            holder.state.setImageResource(R.drawable.process_state_spz);
            //审批情况提示文本
            holder.processStateName.setText(formApprovePerson.personName + " 等待");
            //审批中
        } else if (formApprovePerson.state == FormAddProcessFragment.SHEN_PI_STATE_ZHENG_ZAI_SHEN) {
            //正在审批不加载审批意见及图片
            holder.layout.setVisibility(View.GONE);
            holder.time.setVisibility(View.GONE);
            //审批状态图片
            holder.state.setImageResource(R.drawable.process_state_wait);
            //审批情况提示文本
            holder.processStateName.setText(formApprovePerson.personName + " 正在审批");
            //审批通过
        } else if (formApprovePerson.state == FormAddProcessFragment.SHEN_PI_STATE_TONG_GUO) {
            //审批状态图片
            holder.state.setImageResource(R.drawable.process_state_success);
            //审批情况提示文本
            holder.processStateName.setText(formApprovePerson.personName + " 审批通过");
            //审批时间
            if (StringUtils.isNoEmpty(formApprovePerson.endTime)) holder.time.setText(formApprovePerson.endTime);
            //加载审批意见及审批图片
            loadSp(holder,formApprovePerson);
            //审批拒绝
        } else if (formApprovePerson.state == FormAddProcessFragment.SHEN_PI_STATE_JU_JUE) {
            //审批状态图片
            holder.state.setImageResource(R.drawable.process_state_refuse);
            //审批情况提示文本
            holder.processStateName.setText(formApprovePerson.personName + " 审批拒绝");
            //审批时间
            if (StringUtils.isNoEmpty(formApprovePerson.endTime)) holder.time.setText(formApprovePerson.endTime);
            loadSp(holder,formApprovePerson);     //加载审批意见及审批图片
            //转交
        } else if (formApprovePerson.state == FormAddProcessFragment.SHEN_PI_STATE_ZHUAN_JIAO) {
            //转交不加载审批意见及图片
            holder.yiJian.setVisibility(View.GONE);
            holder.recycler.setVisibility(View.GONE);
            //审批状态图片
            holder.state.setImageResource(R.drawable.process_state_zj);
            //审批情况提示文本
            holder.processStateName.setText(formApprovePerson.personName + " 转交");
            //审批时间
            if (StringUtils.isNoEmpty(formApprovePerson.endTime)) holder.time.setText(formApprovePerson.endTime);
        } else if (formApprovePerson.state == FormAddProcessFragment.SHEN_PI_STATE_PING_LUN) {
            //加载评论文本及图片
            loadSp(holder,formApprovePerson);
            //评论图片隐藏
            holder.state.setVisibility(View.GONE);
            //评论情况提示文本
            holder.processStateName.setText(formApprovePerson.personName + " 评论");
            //评论时间
            if (StringUtils.isNoEmpty(formApprovePerson.endTime)) holder.time.setText(formApprovePerson.endTime);
        } else {
            //加载评论文本及图片
            loadSp(holder,formApprovePerson);
            //评论图片隐藏
            holder.state.setVisibility(View.GONE);
            //评论情况提示文本
            holder.processStateName.setText(formApprovePerson.personName);
            //评论时间
            if (StringUtils.isNoEmpty(formApprovePerson.endTime)) holder.time.setText(formApprovePerson.endTime);
        }
    }

    /**
     * 加载审批意见和审批图片
     * @param holder
     * @param formApprovePerson
     */
    private void loadSp(ApprovePersonResultAdapter.ViewHolder holder, ModeFormFullApprovePerson formApprovePerson){
        holder.layout.setVisibility(View.VISIBLE);
        //审批意见
        if (StringUtils.isNoEmpty(formApprovePerson.opinion)) {
            holder.yiJian.setVisibility(View.VISIBLE);
            holder.yiJian.setText(formApprovePerson.opinion);
        } else {
            holder.yiJian.setVisibility(View.GONE);
        }

        //加载审批图片
        if (formApprovePerson.image != null && StringUtils.isNoEmpty(formApprovePerson.image.trim())) {
            holder.recycler.setVisibility(View.VISIBLE);
            String[] urls = formApprovePerson.image.trim().split(",");
            List<MLocalMedia> localMediaList = new ArrayList<>();
            for (String url : urls) {
                String fileUrl = COS.getInstance().getFileUrl(FormBuilder.BUCKET_NAME, url);
                MLocalMedia localMedia = MLocalMedia.generateHttpAsLocalMedia(fileUrl);
                localMediaList.add(localMedia);
            }
            if (!localMediaList.isEmpty()) {
                holder.recycler.setVisibility(View.VISIBLE);
                holder.recycler.getTitleView().setVisibility(View.GONE);
                holder.recycler.setMode(LoadImagesRecycler.NETWORK)
                        .setData(localMediaList)
                        .show();
            } else {
                holder.recycler.setVisibility(View.GONE);
            }
        } else {
            holder.recycler.setVisibility(View.GONE);
        }

        if (holder.recycler.getVisibility() == View.GONE && holder.yiJian.getVisibility() == View.GONE) {
            holder.layout.setVisibility(View.GONE);
        }
    }


    @Override
    public int getItemCount() {
        return personList == null ? 0 : personList.size();
    }

    public static class ViewHolder extends  RecyclerView.ViewHolder{
        private final TextView processName,processStateName,time,yiJian,personName;
        private final LoadImagesRecycler recycler;
        private final ImageView head,state;
        private final RelativeLayout rl;
        private final LinearLayout layout;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            processName = itemView.findViewById(R.id.adapter_approve_person_result_process_name);
            processStateName = itemView.findViewById(R.id.adapter_approve_person_result_state_text);
            personName = itemView.findViewById(R.id.adapter_approve_person_result_person_name);
            time = itemView.findViewById(R.id.adapter_approve_person_result_time);
            yiJian = itemView.findViewById(R.id.adapter_approve_person_result_shen_pi_yi_jian);
            recycler = itemView.findViewById(R.id.adapter_approve_person_result_shen_pi_tu_pian);
            head = itemView.findViewById(R.id.adapter_approve_person_result_head_person);
            state = itemView.findViewById(R.id.adapter_approve_person_result_state);
            rl = itemView.findViewById(R.id.adapter_approve_person_result_rl);
            layout = itemView.findViewById(R.id.adapter_approve_person_result_layout);
        }
    }
}
